package org.neowei.image.action;

import java.io.File;

import org.neowei.aio.http.HttpRequest;
import org.neowei.aio.http.HttpResponse;
import org.neowei.aio.http.HttpResponseStatus;
import org.neowei.common.Cacheable;
import org.neowei.common.NioFileHelper;
import org.neowei.common.YuanBaseutil;
import org.neowei.common.cacheable.MapCacheableFactory;
import org.neowei.image.api.ImageOption;
import org.neowei.image.api.impl.ImageApiImpl;
import org.neowei.image.build.ImageBuilder;

public class ImageAction {
	private ImageBuilder builder = ImageBuilder.getInstance();

	public void normal(HttpRequest request, HttpResponse response) {
		ImageOption imageOption = builder.build();
		imageOption.getNormal(request.getParams());
		try {
			byte[] bytes = NioFileHelper.getByte(new File(new StringBuilder(imageOption.getSrcpath()).append(File.separatorChar).append(imageOption.getDeeppath()).append(File.separatorChar)
					.append(imageOption.getPicfilename()).toString()));
			response.setContentType("image/jpeg; charset=UTF-8");
			response.write(bytes);
		} catch (Exception e) {
			this.doImageException(response, e, "org.neowei.image.action.ImageAction", imageOption);
		}
	}

	public void crop(HttpRequest request, HttpResponse response) {
		ImageOption imageOption = this.builder.build();
		imageOption.getCrop(request.getParams());
		imageOption.setCroptype("crop");
		ImageApiImpl imageApi = this.builder.yuanObjectPool.get();
		try {
			byte[] bytes = imageApi.doCropImage(imageOption);
			response.setContentType("image/jpeg; charset=UTF-8");
			response.write(bytes);
		} catch (Exception e) {
			this.doImageException(response, e, "org.neowei.image.action.ImageAction", imageOption);
		}
	}

	public void resize(HttpRequest request, HttpResponse response) {
		ImageOption imageOption = builder.build();
		imageOption.getResize(request.getParams());
		imageOption.setCroptype("resize");
		ImageApiImpl imageApi = this.builder.yuanObjectPool.get();
		try {
			byte[] bytes = imageApi.doResizeImage(imageOption);
			response.setContentType("image/jpeg; charset=UTF-8");
			response.write(bytes);
		} catch (Exception e) {
			this.doImageException(response, e, "org.neowei.image.action.ImageAction", imageOption);
		}
	}

	public void bgresize(HttpRequest request, HttpResponse response) {
		ImageOption imageOption = builder.build();
		imageOption.getBgresize(request.getParams());
		imageOption.setCroptype("bgresize");
		ImageApiImpl imageApi = this.builder.yuanObjectPool.get();
		try {
			byte[] bytes = imageApi.doBGResizeImage(imageOption);
			response.setContentType("image/jpeg; charset=UTF-8");
			response.write(bytes);
		} catch (Exception e) {
			this.doImageException(response, e, "org.neowei.image.action.ImageAction", imageOption);
		}
	}

	public void water(HttpRequest request, HttpResponse response) {
		ImageOption imageOption = builder.build();
		imageOption.getWater(request.getParams());
		imageOption.setCroptype("water");
		ImageApiImpl imageApi = this.builder.yuanObjectPool.get();
		try {
			byte[] bytes = imageApi.doWaterImage(imageOption);
			response.setContentType("image/jpeg; charset=UTF-8");
			response.write(bytes);
		} catch (Exception e) {
			this.doImageException(response, e, "org.neowei.image.action.ImageAction", imageOption);
		}
	}

	public void cresize(HttpRequest request, HttpResponse response) {
		ImageOption imageOption = this.builder.build();
		imageOption.getCrop(request.getParams());
		imageOption.setCroptype("cresize");
		ImageApiImpl imageApi = this.builder.yuanObjectPool.get();
		try {
			byte[] bytes = imageApi.doCropResizeImage(imageOption);
			response.setContentType("image/jpeg; charset=UTF-8");
			response.write(bytes);
		} catch (Exception e) {
			this.doImageException(response, e, "org.neowei.image.action.ImageAction", imageOption);
		}
	}

	private void doImageException(HttpResponse response, Exception e, String message, ImageOption imageOption) {
		Cacheable cacheable = MapCacheableFactory.getInstance().create();
		String errorimage = cacheable.getmap("img404.pathAndName");
		try {
			if (errorimage == null || errorimage.length() < 5) {
				response.setHttpResponseStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
				response.write(YuanBaseutil.exceptionToJson(message, "imageOption values:" + imageOption.toString(), e).getBytes());
			} else {
				imageOption.setDeeppath("");
				imageOption.setPicfilename("404.jpg");
				imageOption.setDespath(errorimage.replaceAll("404.jpg", ""));
				ImageApiImpl imageApi = this.builder.yuanObjectPool.get();
				byte[] bytes = imageApi.doResizeImage(imageOption);
				// byte[] bytes = NioFileHelper.getByte(new
				// File(cacheable.getmap("img404.pathAndName")));
				response.setContentType("image/jpeg; charset=UTF-8");
				response.write(bytes);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			cacheable.returnObject();
		}
	}

}
